﻿using System;
using System.Collections.Generic;

public interface IHandler
{
    void RegisterHandler(HandlerCenter center);
}
public delegate void NetHandler(byte[] data);//消息接收器中所有方法的模板委托

public class HandlerCenter
{
    //存储了所有protocol+对应函数
    Dictionary<Protocol, NetHandler> handlers = new Dictionary<Protocol, NetHandler>();

    public HandlerCenter()
    {
        LoginHandler login = new LoginHandler();
        login.RegisterHandler(this);

        GameHandler game = new GameHandler();
        game.RegisterHandler(this);
    }

    public void AddHandler(Protocol cmd,NetHandler handler)
    {
        handlers.Add(cmd,handler);
    }

    public void OnReceive(ByteBuffer buffer)
    {
        Protocol cmd = (Protocol)buffer.ReadInt();
        byte[] data = buffer.ReadBytes();

        var func = handlers[cmd];
        func(data);
        buffer.Close();
    }
}
